<?php

namespace App\Admin\Controllers;

use App\Models\Administrator;
use App\Models\Category;
use App\Models\Machine;

use App\Models\Operator;
use App\Models\Product;
use App\Models\Proxy;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class MachinesController extends Controller
{
    use ModelForm;

    private $header = '机器管理';
    private $description = '管理所有旗下机器';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description($this->description);
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->form());
        });
    }

    public function show(Machine $machine)
    {
        return Admin::content(function (Content $content) use ($machine) {
            $content->header('查看详情');
            $content->body(view('admin.machines.show', ['machine' => $machine]));
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Machine::class, function (Grid $grid) {
            if (Admin::user()->type === Administrator::OPERATOR) {
                $grid->model()->where('operator_id', Admin::user()->id);
            }
            $grid->name('名称');
            $grid->uuid('机器号');
            $grid->operator()->name('运营');
            $grid->actions(function ($actions) {
                $actions->prepend('<a href="' . route('admin.machines.show', [$actions->getKey()]) . '"><i class="fa fa-eye"></i></a>');
            });
            $grid->filter(function ($filter) {
                // 去掉默认的id过滤器
                $filter->disableIdFilter();

            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Machine::class, function (Form $form){
            $form->text('name', '名称')->rules('required');
            $form->text('uuid', 'UUID')->rules('required');
            if (Admin::user()->type == Administrator::OPERATOR) {
                $operators = Operator::where('id', Admin::user()->id)->pluck('name', 'id');
            } else {
                $operators = Operator::all()->pluck('name', 'id');
            }
            $form->select('operator_id', '运营')->options($operators);
            $products = Product::query()->pluck('name', 'id');
            $form->listbox('products', '商品')->options($products);
            $category = Category::query()->pluck('name', 'id');
            $form->listbox('categories', '分类')->options($category);
        });
    }
}
